package com.aaa.phone.service.LoginService.impl;

import com.aaa.entity.AppUserinfo;
import com.aaa.entity.AppUserlogin;
import com.aaa.phone.resultcommon.Result;
import com.aaa.phone.dao.logindao.ILoginDao;
import com.aaa.phone.service.LoginService.ILoginService;
import com.aaa.phone.systemconst.ISystemConst;
import com.aaa.util.MessageUtil;
import com.aaa.web.shiro.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @program: batterysharing
 * @description: 登录业务
 * @author: 王璐
 * @create: 2018-12-15 16:32
 **/
@Service
public class LoginServiceImpl implements ILoginService,ISystemConst {
    /**
     * 自动注入
     */
    @Autowired
    private ILoginDao dao;

    /**
     * @Description: 用户登录
     * @Author: 王璐
     * @Date: 2018/12/17
     */
    @Override
    public int userLogin(String tel,String psw) {
        Result result = new Result();
        //判断手机号是否存在,返回一个对象
        AppUserlogin userExist = dao.userExist(tel);
        if (userExist != null) {
            //加密后的密码
            String password = ShiroUtil.getPassword(userExist.getSalt(),psw);
            //数据库中的密码
            String beforePsw = userExist.getPassword();
            if(beforePsw.equals(password)){
                //用户手机号和密码输入正确，登陆成功，返回成功码100
                result.setCode(SUCCESS_CODE);
            }else {
                //用户密码输入错误，返回错误码202
                result.setCode(PSWERROR);
            }
        } else {
            //用户手机号错误，返回错误码203
            result.setCode(TEL_NOT_EXIST);
        }
        return result.getCode();
    }
    /**
     * @Description: 用户验证码登录，点击获取验证码，判断用户是否存在
     * @Author: 王璐
     * @Date: 2018/12/18
    */
    @Override
    public int loginByCode(String tel) {
        AppUserlogin exist = dao.userExist(tel);
        if(exist == null){
            //用户不存在，返回失败码，提醒用户去注册
            return DEFEAT_CODE;
        }else{
            //用户存在，返回验证码
            int code = Integer.parseInt(MessageUtil.getMessageCode(tel));
            return code;
        }
    }

    /**
     * @Description: 获取用户信息
     * @Author: 王璐
     * @Date: 2018/12/18
     */
    @Override
    public AppUserinfo getUserInfo(String tel) {
       List<AppUserinfo> userInfo = dao.getUserInfo(tel);
       return userInfo.get(ZERO);
    }
}
